package cn.sc.allinpay.allfavour.business.common.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.AppCompatTextView;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;

import java.math.BigDecimal;

import cn.pai.common.utils.AmtUtils;
import cn.sc.allinpay.allfavour.R;
import cn.sc.allinpay.allfavour.base.widget.CashierInputFilter;
import cn.sc.allinpay.allfavour.business.common.adapter.CouponAdapter;
import cn.sc.allinpay.allfavour.business.common.data.CouponsData;

/**
 * description：卡券金额输入弹出框
 * author：pany
 * on 2018/7/28 17:36
 */
public class CouponInputAmtDialog extends Dialog implements View.OnClickListener {

    private CouponsData data;
    private BigDecimal maxAmt;//最大可用金额
    private BigDecimal balAmt;//最大可用余额
    private BigDecimal paidAmt;// 待支付金额
    private AppCompatTextView tvMaxAmt;
    private AppCompatTextView tvUseAmt;
    private AppCompatEditText etAmt;
    private OnDialogListener onDialogListener;

    public CouponInputAmtDialog(@NonNull Context context, CouponsData data, CouponAdapter adapter) {
        super(context);
        this.data = data;
        this.balAmt = new BigDecimal(AmtUtils.changeF2Y(data.getBalance()));
        if (data.getLimit() != null && !data.getLimit().getMaxAmt().equals("null") ) {
            BigDecimal oldMaxAmt = new BigDecimal(AmtUtils.changeF2Y(data.getLimit().getMaxAmt()));//剩余金额
            this.maxAmt = oldMaxAmt.subtract(adapter.getUseAmtType0(data.getTemplateCode()));
        } else {
            this.maxAmt = this.balAmt;
        }

        SharedPreferences settings = context.getSharedPreferences("paidAmt", 0);
        this.paidAmt = new BigDecimal(settings.getString("paidAmt","-1"));
    }

    public CouponInputAmtDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_coupon_input_amt);

        tvMaxAmt = findViewById(R.id.tvMaxAmt);
        tvUseAmt = findViewById(R.id.tvUseAmt);

//        tvMaxAmt.setText("最高可用金额(￥):  " + AmtUtils.formatY(maxAmt));
        tvUseAmt.setText("剩余可用金额(￥):  " + AmtUtils.formatY(balAmt));
        etAmt = findViewById(R.id.etAmt);
        etAmt.setText(data.getUseAmt());
        if (BigDecimal.ZERO.compareTo(paidAmt) > 0) {
            InputFilter[] filters = {new CashierInputFilter(
                    balAmt.toString())};
            etAmt.setFilters(filters);
        } else {
            paidAmt = paidAmt.add(new BigDecimal(data.getUseAmt()));
            InputFilter[] filters = {new CashierInputFilter(balAmt.min(paidAmt).toString())};
            etAmt.setFilters(filters);
        }

        etAmt.setSelection(etAmt.getText().length());
        findViewById(R.id.btnConfirm).setOnClickListener(this);
        findViewById(R.id.btnCancel).setOnClickListener(this);
    }

    public void setOnDialogListener(OnDialogListener onDialogListener) {
        this.onDialogListener = onDialogListener;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnConfirm:
                dismiss();
                if (onDialogListener != null) {
                    String amt = etAmt.getText().toString().trim();
                    if (TextUtils.isEmpty(amt) || BigDecimal.ZERO.compareTo(new BigDecimal(amt)) == 0) {
                        onDialogListener.onConfirm(null);
                    } else {
                        onDialogListener.onConfirm(amt);
                    }
                }
                break;
            case R.id.btnCancel:
                dismiss();
                if (onDialogListener != null) {
                    onDialogListener.onCancel();
                }
                break;
        }
    }

    public static abstract class OnDialogListener {

        protected abstract void onConfirm(String amt);

        protected void onCancel() {
        }
    }
}
